package com.szd.qiongyou20.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.activity.SearchActivity;
import com.szd.qiongyou20.home.adapter.ViewPager_Main_Adapter;
import com.szd.qiongyou20.community.fragment.Fragment_Community;
import com.szd.qiongyou20.home.fragment.Fragment_Destination;
import com.szd.qiongyou20.home.fragment.Fragment_Home;
import com.szd.qiongyou20.home.fragment.Fragment_Mall;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Context mContext = this;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mList = new ArrayList<>();
    private String[] mStrings_tab_titles;
    private ImageView mImageView_search;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    /**
     * 初始化数据的方法
     */
    private void initData() {
        // 获取首页的TabTitle中的数据
        mStrings_tab_titles = getResources().getStringArray(R.array.tab_title_main);

        // 初始化碎片, 并添加至集合中

        // 初始化首页的碎片
        Fragment_Home fragment_home = new Fragment_Home();
        mList.add(fragment_home);

        // 初始化目的地的碎片
        Fragment_Destination fragment_destination = new Fragment_Destination();
        mList.add(fragment_destination);

        // 初始化商城的碎片
        Fragment_Mall fragment_mall = new Fragment_Mall();
        mList.add(fragment_mall);

        // 初始化社区的碎片
        Fragment_Community fragment_community = new Fragment_Community();
        mList.add(fragment_community);

        // 初始化适配器
        ViewPager_Main_Adapter adapter = new ViewPager_Main_Adapter(getSupportFragmentManager(), mList, mStrings_tab_titles);
        // 绑定ViewPager的适配器
        mViewPager.setAdapter(adapter);
        // 给TabLayout绑定ViewPager
        mTabLayout.setupWithViewPager(mViewPager);

    }
    /**
     * 初始化View控件
     */
    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout_main);
        mViewPager = (ViewPager) findViewById(R.id.viewPager_main);
        mViewPager.setOffscreenPageLimit(4);

        mImageView_search = (ImageView) findViewById(R.id.search_main);

        mImageView_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                startActivity(intent);
            }
        });
    }
}
